package cn.admin.config.common.protocol;

import com.caucho.hessian.io.AbstractHessianOutput;
import com.caucho.hessian.io.AbstractSerializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

class ProtocolPacketEncoder extends AbstractSerializer {

    @Override
    public void writeObject(Object obj, AbstractHessianOutput out) throws IOException {
        if (!out.addRef(obj)) {
            ProtocolPacket pp = (ProtocolPacket) obj;
            List<ProtocolElement> elements = new ArrayList<>();
            for (ProtocolElement element : pp) {
                elements.add(element);
            }

            out.writeMapBegin(pp.getClass().getName());
            out.writeString("elements");
            out.writeObject(elements);
            out.writeMapEnd();
        }
    }
}
